<template>
  <div class="updateUser">
    <!-- 标题 -->
    <h3 class="title">修改信息</h3>
    <!-- 表单 -->
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="密码" prop="passWord">
        <el-input type="password"  v-model="ruleForm.passWord" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">修改</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import userRequest from "../request/user";
export default {
  data() {
    var passWord = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (/^[a-z0-9]{5,12}$/.test(value)) {
          callback();
        } else {
          callback(new Error("密码必须为字母开头6-12位数字字母组成"));
        }
      }
    };
    var checkPass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入再次输入密码"));
      } else if(value !== this.ruleForm.passWord){
         callback(new Error("两次密码不一致！"));
      }
    };
    return {
      userinfo: null,
      ruleForm: {
        passWord: "",
        checkPass: "",
      },
      rules: {
        passWord: [{ validator: passWord, trigger: "blur" }],
        checkPass: [{ validator: checkPass, trigger: "blur" }],
      }
    };
  },

   methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          userRequest.updateUser({
            id : this.userinfo.id,
            passWord : this.ruleForm.passWord,
          })
          .then(res => {
              if (res.data.success == true) {
                this.$message.success("修改成功!");
                this.resetForm();
              } else {
                this.$message.error(res.data.message);
              }
            });
        } else {
          console.log("修改失败!!");
          return false;
        }
      });
    },

    resetForm() {
    this.$refs.ruleForm.resetFields();
  }
  },

  created() {
     this.userinfo = JSON.parse(window.localStorage.getItem("userinfo")).user;
  }
};
</script>

<style lang="scss" scoped>
.updateUser {
  overflow: hidden;
  margin-top: 50px;

  ::v-deep .title {
    text-align: center;
    font-size: 20px;
  }
  ::v-deep .demo-ruleForm {
    width: 500px;
    margin: 30px auto;
  }
  ::v-deep .el-input__inner {
    height: 45px;
    font-size: 16px;
  }
  ::v-deep .el-form-item__label {
    font-size: 16px;
    position: relative;
    top: 10px;
  }
  ::v-deep .el-form-item__content {
    margin-top: 10px;
    height: 50px;
    display: flex;
  }
  ::v-deep .el-form-item__error {
    font-size: 16px;
  }

  ::v-deep .el-select-dropdown__item {
    display: flex;
    justify-content: center;
  }
  ::v-deep .el-radio-group {
    width: 100px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: -8px;
  }
}
</style>